package evo.springboot.controller;

import evo.springboot.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class userController {

    @RequestMapping(value = "/each/list")
    public ModelAndView eachList(){
        List<User> userList = new ArrayList();
        for(int i = 0;i < 10;i++){
            User user = new User(01+i,"李"+i,25+i);
            userList.add(user);
        }
        ModelAndView mv = new ModelAndView();
        mv.addObject("userList",userList);
        mv.setViewName("eachList");
        return mv;
    }

    @RequestMapping(value = "/each/map")
    public ModelAndView eachMap(){
        Map<Integer,User> userMap = new HashMap();
        for(int i = 0;i < 10;i++){
            User user = new User(01+i,"李"+i,25+i);
            userMap.put(i,user);
        }
        ModelAndView mv = new ModelAndView();
        mv.addObject("userMap",userMap);
        mv.setViewName("eachMap");
        return mv;
    }

    @RequestMapping(value = "/each/array")
    public ModelAndView eachArray(){
        User[] userArray = new User[10];
        for(int i = 0;i < 10;i++){
            User user = new User(01+i,"李"+i,25+i);
            userArray[i] = user;
        }
        ModelAndView mv = new ModelAndView();
        mv.addObject("userArray",userArray);
        mv.setViewName("eachArray");
        return mv;
    }

    @RequestMapping(value = "/each/all")
    public String eachAll(Model model) {
        //list -> Map -> List -> User
        List<Map<Integer, List<User>>> myList = new ArrayList();

        for (int i = 0; i < 2; i++) {

            Map<Integer, List<User>> myMap = new HashMap();
            for (int j = 0; j < 2; j++) {
                List<User> myUserList = new ArrayList();
                for (int k = 0; k < 3; k++) {
                    User user = new User(k,"张三" + k,25+k);
                    myUserList.add(user);
                }
                myMap.put(j, myUserList);
            }
            myList.add(myMap);

        }
        model.addAttribute("myList", myList);
        return "eachAll";
    }
}
